<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:14 $)
// $RCSfile: index.php,v $ : $Revision: 1.8 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

define("SWIFT_AREA", 30);

if (defined("SWIFTCUSTOMPATH"))
{
	chdir(SWIFTCUSTOMPATH);
} else {
	chdir('./../');
}
require_once ("./swift.php");

if ($_REQUEST["_ca"] == "login") {
	/*
	* #########################################################
	* Authenticate User, Insert session and redirect else just display login form
	* #########################################################
	*/
	// Remember Password available?
	if (!empty($_POST["cookiepassword"]) && $_POST["password"] == "******")
	{
		// Try to use cookie password
		$staffid = $staffauth->authenticate($_POST["username"], $_POST["cookiepassword"], true, false);
	} else {
		$staffid = $staffauth->authenticate($_POST["username"], $_POST["password"], true);
	}
	if (!empty($staffid))
	{
		require_once ("./includes/functions_html.php");

		// Authentication successfull, Generate a sessionid for this user
		$session->insertSession(SESSION_ADMIN, $staffid);

		// We always empty the remember cookie if username differs
		$_cookieusername = $cookie->getSCookie("remusernameadm");
		if ($_POST["username"] != $_cookieusername && !empty($_cookieusername))
		{
			$cookie->emptySCookie("remusernameadm");
			$cookie->emptySCookie("rempasswordadm");
		}

		if ($_POST["remember"] == 1 && $_POST["password"] != "******")
		{
			$cookie->setSCookie("remusernameadm", $_POST["username"], true);
			$cookie->setSCookie("rempasswordadm", md5($_POST["password"]), true);
		} else if ($_POST["remember"] == 1 && $_POST["password"] == "******") {
			// Do Nothing
		} else {
			$cookie->emptySCookie("remusernameadm");
			$cookie->emptySCookie("rempasswordadm");
		}

		printRedirect($_SWIFT["language"]["redirect_login"], "index.php?_m=core&_a=dashboard", true);
	} else {
		$cookie->emptySCookie("remusernameadm");
		$cookie->emptySCookie("rempasswordadm");
		$staffauth->displayLoginForm($_SWIFT["errorstr"]);
	}
} else if ($_REQUEST["_ca"] == "logout") {
	$result = $session->logoutSession(SESSION_ADMIN);

	$staffauth->displayLoginForm(iif($result, $_SWIFT["language"]["loggedout"]));
} else {
	if (!$session->start(SESSION_ADMIN)) {
		/*
		* #########################################################
		* No other action, we always try to start a session
		* #########################################################
		*/
		// Failed to load session
		$staffauth->displayLoginForm($_SWIFT["errorstr"]);
		exit;
	}

	// User logged in, trigger events
	if (!$events->triggerEvent(EVENT_ADMIN))
	{
		require_once ("./includes/functions_html.php");
		printRedirect($_SWIFT["language"]["redirect_dashboard"], "index.php?_m=core&_a=dashboard", true);
	}
}
?>